<?php
	include ROOT_CLASSES.'/DB/Connection.php';
	
	class Table
	{
		protected $connection;
		
		protected $api;
		
		protected $table;
		
		protected $query;
		
		protected $column;
		
		protected $where;
		
		protected $orderby;
		
		protected $groupby;
		
		protected $having;
        
        protected $describe;
        
        protected $columnUpdate;
        
        protected $columnInsert;
		
		public function __construct($table=null)
		{
			$this->connection=new Connection();
			$this->connection->Open();
			$this->connection->ConnectDB();
			$this->table=$table;
		}
		
		public function setTable($table)
		{
			$this->table=$table;
		}
		
		public function getTable()
		{
			return $this->table;
		}

		public function select($type_data='assoc',$column="*",$condition=null,$orderby=null,$groupby=null,$having=null)
		{
			$data=null;
			$this->setColumn($column);
			$this->setWhere($condition);
			$this->setOrderBy($orderby);
			$_orderby=$this->getOrderBy();
			if(!empty($_orderby))
			{
				$_orderby= $_orderby;
			}
			$query='SELECT '.$this->getColumn().' FROM '.$this->getTable().' WHERE '.$this->getWhere()." ".$_orderby;	
			$this->setQuery($query);
			if($type_data=='assoc')
			{
			  
				$data=$this->getAssoc($this->getQuery());
                
			}
			else
			{
				
			}
			return $data;
		}
		
		public function getResult($sql='',$error='Truy vấn thất bại')
		{
			if(!empty($sql))
			{
				$rs=mysql_query($sql, $this->connection->getConnection()) or die($error);
			}
			else
			{
				$rs=mysql_query($this->getQuery(), $this->connection->getConnection()) or die($error);
			}
			return $rs;
		}
		
		public function getRecords($data)
		{
			$record=0;
			if(is_array($data))
			{
				$record=count($data);
			}
			else
			{
				$record=mysql_num_rows($data);
			}
			return $record;
		}
		/*
		* Phương thức trả về mảng dữ liệu theo kiểu chỉ mục
		*/
		public function getAssoc($sql='',$error=null)
		{
			$data=null;
			$rs=$this->getResult($sql,$error);
			if($rs)
			{
				if($this->getRecords($rs) > 0)
				{
					while($d=mysql_fetch_assoc($rs))
					{
						$data[]=$d;
					}
				}
			}
			return $data;
		}
		
		public function setColumn($column)
		{
			if(is_array($column))
			{
				$this->column = implode(',',$column);
			}
			else
			{
				$this->column='*';
			}
			$this->column=rtrim($this->column,',');
		}
		
		public function getColumn()
		{
			return $this->column;
		}
		
		public function setQuery($query)
		{
			$this->query= $query;
		}
		
		public function getQuery()
		{
			return $this->query;
		}
		
		public function setWhere($condition)
		{
			$this->where='1';
			if(is_array($condition))
			{
				foreach($condition as $k => $c)
				{
					$this->where .= ' AND '.$k.'="'.$c.'"';
				}
			}else{
			     if($condition != null)
			         $this->where = $condition; // Example :  id = '".$id."';
			}
		}
		
		public function getWhere()
		{
			return $this->where;
		}
		
		public function setOrderBy($orderby)
		{
			$this->orderby='';
			if(is_array($orderby))
			{
				foreach($orderby as $p => $o)
				{
					$this->orderby .= $p.' '.$o.',';
				}
			}
			$this->orderby=rtrim($this->orderby,',');
		}
		
		public function getOrderBy()
		{
			return $this->orderby;
		}
		
		public function setGroupBy($groupby)
		{
			$this->groupby='';
			if(is_array($orderby))
			{
				foreach($orderby as $g)
				{
					$this->groupby .= $g.',';
				}
			}
			$this->groupby=rtrim($this->groupby,',');
		}
		
		public function getGroupBy()
		{
			return $this->groupby;
		}
        
        public function setColumnUpdate($column){
            $this->columnUpdate = "";
            
            if(is_array($column)){
                foreach($column as $key => $c){
                    $col[] .= $key."=".'"'.$c.'"';
                }
                $this->columnUpdate = implode(',',$col);
            }
        }
        public function getColumnUpdate(){
            return $this->columnUpdate;
        }
        public function setColumnInsert($column){
            $this->columnInsert = "";
            $this->column = "";
            if(is_array($column)){
                foreach($column as $key => $c){
                    $col[] .= $key;
                    $value[] .= '"'.$c.'"';
                }
                $this->column =  implode(",",$col);
                $this->columnInsert = implode(",",$value);
            }
        }
        public function getColumnInsert(){
            return $this->columnInsert;
        }
		/**
        *   Hàm Insert Truyền vào 
        *       - Column là mãng với dạng array("tên cột 1"=>"Giá trị update 1","tên cột 2"=>"Giá trị update 2")
        */
        public function insert($column = null){
                $this->setColumnInsert($column);
				$this->setQuery('INSERT INTO ' . $this->table .'('. $this->getColumn() .') VALUE ('.$this->getColumnInsert().')');
				$this->result = $this->getResult();
				return $this->result;
		}
         /**
            *   Hàm update Truyền vào 
            *       - Điều kiện là một mãng hoặc một chuổi điều kiện dạng sql 
            *       - Column là mãng với dạng array("tên cột 1"=>"Giá trị update 1","tên cột 2"=>"Giá trị update 2")
            */
        public function update($condition=null,$column = null){
			$this->setWhere($condition);
			$this->setColumnSave($column);
			$this->setQuery('UPDATE ' . $this->table . ' SET ' . $this->getColumnSave($column) . ' WHERE '.$this->getWhere($condition));
			$this->result = $this->getResult();
			return $this->result;
		}
        public function delete($condition = null){
            $this->setWhere($condition);
			$this->setQuery('DELETE FROM ' . $this->table .' WHERE '.$this->getWhere());
			$this->result = $this->getResult();
			return $this->result;            
        }
	}
?>